package com.godme.rocket.recycle;


import com.godme.rocket.repo.GoodsRepository;
import com.godme.rocket.repo.OrderRepository;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

@Component
@RocketMQMessageListener(topic = "shopping", consumerGroup = "recycle")
public class GoodsRecycleListener implements RocketMQListener<MessageExt> {


    @Autowired
    OrderRepository orderRepository;


    @Autowired
    GoodsRepository goodsRepository;

    final AtomicInteger unPayedSequence = new AtomicInteger(0);

    @Override
    public void onMessage(MessageExt messageExt) {
        String orderId = new String(messageExt.getBody());
        Boolean payed = orderRepository.queryStatusByOid(orderId);
        if(payed){
            System.err.println(orderId + "\t 付款成功");
        } else {
            goodsRepository.incrementProduct();
            System.err.println(unPayedSequence.incrementAndGet() + "\t"+orderId+"未付款，库存+1");
        }
    }
}
